#include "soda.h"

#include <Windows.h>
#include <iostream>
#include <fstream>

using namespace std;


#include "../TPackage/lzma-helper.h"



soda::soda(void)
{
}


soda::~soda(void)
{
}

bool soda::extract(const wchar_t* path)
{
	// extract content and save to dsik
	// http://msdn.microsoft.com/en-us/library/windows/desktop/ms648008(v=vs.85).aspx#_win32_Updating_Resources

	using namespace std;

	HMODULE hInstance = LoadLibraryEx(L"T509", NULL, LOAD_LIBRARY_AS_DATAFILE);
	HRSRC hResInfo = FindResource(hInstance, L"\"SODA.*\"", RT_RCDATA);
	if(!hResInfo)
	{
		int hr = GetLastError();
		cout << hr << endl;
	}

	HGLOBAL hRes = LoadResource(hInstance, hResInfo);
	LPVOID memRes = LockResource(hRes);
	DWORD sizeRes = SizeofResource(hInstance, hResInfo);

	// test
	/*{
		ofstream of(path + L"__", ios::binary);
		of.write((const char*)memRes, sizeRes);
		of.close();
	}*/

	bool result = LzmaHelper::DecompressMemory((const unsigned char*)memRes, sizeRes, path);
	FreeLibrary(hInstance);

    // make file hidden
    SetFileAttributes(path, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED);

	return result;
}